<?php

namespace App\Http\Controllers\Lottery;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    //获取图片
    public function getPicture(Request $request){
        $type = $request->get('type');
        $data = DB::table('picture')->where('type','=',$type)->orderby('id','desc')->limit(5)->get();
        if(!$data){
            return response()->json(['msg'=>'0','data'=>'获取失败']);
        }
        return response()->json(['msg'=>'1','data'=>$data]);
    }
    //获取往期数据
    public function getLotteryNum(Request $request){
        $type =  $request->get('type');
        $limit = $request->get('limit')&&is_numeric($request->get('limit')) ?  $request->get('limit')  : 20;
        switch($type) {
            case '1':
                $data = $this->dogetLotteryNum('黑龙江11选5',$limit);
                break;
            case '2':
                $data = $this->dogetLotteryNum('黑龙江快乐十分',$limit);
                break;
            case '3':
                $data = $this->dogetLotteryNum('吉林快3',$limit);
                break;
            case '4':
                $table = '吉林时时彩';
                $data = $this->dogetLotteryNum($table,$limit);
                foreach ($data as $key =>$value){
                    $re =  $this->jiLinShiShiCai($data[$key]['期号']);
                    $data[$key]['前三和尾单双'] =$re[0];
                    $data[$key]['中三和尾单双'] =$re[1];
                    $data[$key]['后三和尾单双'] =$re[2];
                    $data[$key]['前三和尾大小'] =$re[3];
                    $data[$key]['中三和尾大小'] =$re[4];
                    $data[$key]['后三和尾大小'] =$re[5];
                    $data[$key]['后三跨度'] =$re[6];
                    $data[$key]['前三尾'] =$re[7];
                    $data[$key]['中三尾'] =$re[8];
                    $data[$key]['后三尾'] =$re[9];
                }
                break;
            case '5':
                $data = $this->dogetLotteryNum('四川金7乐',$limit);
                break;
            case '6':
                $data = $this->dogetLotteryNum('四川快乐12',$limit);
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }
        return response()->json(['msg'=>'1','data'=>$data]);
    }

    public function dogetLotteryNum($type,$limit){
        $data = DB::table($type)->select('期号','开奖号','关注号')->orderby('期号','desc')->limit($limit)->get();
        $data = obj2arr($data);
        $result = array();
        foreach($data as $v){
            $v['关注号'] = str_split($v['关注号']);
            $type == '吉林快3'||$type == '吉林时时彩' ? $v['开奖号'] = str_split($v['开奖号']): $v['开奖号'] = explode('-',$v['开奖号']);
            array_push($result,$v);
        }
        foreach ($result as $key =>$value){
            $result[$key]['单号遗漏'] = $this->getMissing($type,$result[$key]['期号']);
            $repeat= $this->getRepeatNum($result[$key]['期号'],$type);
            $span= $this->span($result[$key]['期号'],$type);
            $result[$key]['重号'] = $repeat[0];
            $result[$key]['前三重号'] = $repeat[1];
            $result[$key]['跨度']= $span[0];
            $result[$key]['前跨']= $span[1];
            $result[$key]['和值']= $span[3];
            $result[$key]['前三和值']= $span[2];
            $result[$key]['奇数']= $span[4];
            $result[$key]['大数']= $span[5];
            $result[$key]['和值大小']= $span[6];
        }

        return $result;
    }

    //获取最新中奖号码
    public function getNewWin(Request $request){
        $type =  $request->get('type');
        $sj = $request->get('sj');
        $stage = $request->get('stage');
        if(!is_numeric($stage )|| empty($stage)){
            return response()->json(['msg'=>'0','data'=>'期数格式错误']);
        }
        if(empty($sj)){
            return response()->json(['msg'=>'0','data'=>'关注数为空']);

        }
        if(strlen($sj) != 8){
            return response()->json(['msg'=>'0','data'=>'关注号码长度不对']);
        }
        switch($type) {
            case '1':
                $data = $this->dogetNewWin('黑龙江11选5',$stage,$sj);
                break;
            case '2':
                $data = $this->dogetNewWin('黑龙江快乐十分',$stage,$sj);
                break;
            case '3':
                $data = $this->dogetNewWin('吉林快3',$stage,$sj);
                break;
            case '4':
                $data = $this->dogetNewWin('吉林时时彩',$stage,$sj);
                $re =  $this->jiLinShiShiCai($data['期号']);
                $data['前三和尾单双'] =$re[0];
                $data['中三和尾单双'] =$re[1];
                $data['后三和尾单双'] =$re[2];
                $data['前三和尾大小'] =$re[3];
                $data['中三和尾大小'] =$re[4];
                $data['后三和尾大小'] =$re[5];
                $data['后三跨度'] =$re[6];
                $data['前三尾'] =$re[7];
                $data['中三尾'] =$re[8];
                $data['后三尾'] =$re[9];
                break;
            case '5':
                $data = $this->dogetNewWin('四川金7乐',$stage,$sj);
                break;
            case '6':
                $data = $this->dogetNewWin('四川快乐12',$stage,$sj);
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }
        return response()->json(['msg'=>'1','data'=>$data]);
    }

    public function dogetNewWin($type,$stage,$sj){
        DB::table($type)->where('期号',$stage)->update(['关注号' => $sj]);
        $data = DB::table($type)->select('期号','开奖号','关注号')->where('期号',$stage)->orderby('期号','desc')->first();
        $data = obj2arr($data);
        $data_gzh=[];
        $gzh = explode('-',$data['关注号']);
        foreach($gzh as $v){
            array_push($data_gzh,(int)$v);
        }
        $this->updateRecommend($type,$data['开奖号']);
        $data['关注号'] = $data_gzh;
        $type == '吉林快3'||$type == '吉林时时彩' ? $data['开奖号'] = str_split($data['开奖号']): $data['开奖号'] = explode('-',$data['开奖号']);

        $data['单号遗漏'] = $this->getMissing($type,$data['期号']);
        $repeat= $this->getRepeatNum($data['期号'],$type);
        $span= $this->span($data['期号'],$type);
        $data['重号'] = $repeat[0];
        $data['前三重号'] = $repeat[1];
        $data['跨度']= $span[0];
        $data['前跨']= $span[1];
        $data['和值']= $span[3];
        $data['前三和值']= $span[2];
        $data['奇数']= $span[4];
        $data['大数']= $span[5];
        $data['和值大小']= $span[6];
        return $data;


    }
    //获取开奖时间
    public function getLotteryTime(Request $request){
        $type =  $request->get('type');
        switch($type) {
            case '1':
                $data = $this->dogetLotteryTime('黑龙江11选5');
                break;
            case '2':
                $data = $this->dogetLotteryTime('黑龙江快乐十分');
                break;
            case '3':
                $data = $this->dogetLotteryTime('吉林快3');
                break;
            case '4':
                $data = $this->dogetLotteryTime('吉林时时彩');
                break;
            case '5':
                $data = $this->dogetLotteryTime('四川金7乐');
                break;
            case '6':
                $data = $this->dogetLotteryTime('四川快乐12');
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }
        if(isset($data['tag'])){


            return response()->json(['msg'=>'2','data'=>$data]);
        }
        return response()->json(['msg'=>'1','data'=>$data]);

    }

    public  function  dogetLotteryTime($type){
        $result = obj2arr(DB::table('admin_game')->select('起售时间','全天期数','每期间隔秒数','与北京时间误差秒数')->where('游戏中文名',$type)->first());
        $b_time = strtotime(date('Y-m-d',time()).' '.$result['起售时间']) + $result['与北京时间误差秒数'];
        $next_stage = ceil((time()-$b_time)/$result['每期间隔秒数']);
        $n_time = $b_time + $next_stage*$result['每期间隔秒数']; //下期开奖时间
        $data['next_stage'] = $next_stage;
        $data['ts'] = $result['全天期数'];
        if($next_stage > $result['全天期数']){
            $data['tag'] = 0;
            return $data;
        }
        $data['n_time'] = $n_time - time();
        return $data;
    }


    public function  updateRecommend($table,$string){//$table,$string
        $x = array();
        $y = array();
        $z = array();
        $c = array();
        $v = array();
//       $string = '05 11 10 06 04';
//        $table = '黑龙江11选5';
        $sjn = 0;
        if($table == '黑龙江11选5'){
            $sjn = 11;
        }
        if($table == '黑龙江快乐十分'){
            $sjn = 20;
        }
        if($table == '四川快乐12'){
            $sjn = 12;
        }
        if(!$sjn){
            return ;
        }
        $array =   explode(' ',$string);
        sort($array);
        $r = DB::table('admin_game')->where('游戏中文名',$table)->select('号码推荐')->get();
        preg_match_all("/\[.*?\//",$r[0]->号码推荐,$a);//推荐号
        $a[0][0]=trim($a[0][0],'[/');
        $a[0][1]=trim($a[0][1],'[/');
        $a[0][2]=trim($a[0][2],'[/');
        $a[0][3]=trim($a[0][3],'[/');
        $a[0][4]=trim($a[0][4],'[/');
        preg_match_all("/推出.*?期/",$r[0]->号码推荐,$b);//推出期数
        $b[0][0]=trim($b[0][0],'推出期');
        $b[0][1]=trim($b[0][1],'推出期');
        $b[0][2]=trim($b[0][2],'推出期');
        $b[0][3]=trim($b[0][3],'推出期');
        $b[0][4]=trim($b[0][4],'推出期');
        preg_match_all("/任选.*?\]/",$r[0]->号码推荐,$c);//任选234
        $string1 = $c[0][0];
        $string2 = $c[0][1];
        $string3 = $c[0][2];
        $string4 = $c[0][3];
        $string5 = $c[0][4];
        //任选二
        //中奖的2位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                array_push($x,$array[$i].' '.$array[$j]);
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][0]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $x)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        //随机出2号码
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-1);
                $w = rand($q+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }

                if($w<10){
                    $w = '0'.$w;
                }
                $new1 .= $q.' '.$w.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string1='任选二['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][0].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][0]+1;
            $aa = '/推出'.$aa.'期';
            $string1 = preg_replace("/推出.*?期/",$aa,$string1);
        }
        //任选三1
        //中奖的3位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    array_push($y,$array[$i].' '.$array[$j].' '.$array[$s]);
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][1]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $y)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        //随机出号码
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-2);
                $w = rand($q+1,$sjn-1);
                $e = rand($w+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }
                if($w<10){
                    $w = '0'.$w;
                }
                if($e<10){
                    $e = '0'.$e;
                }
                $new1 .= $q.' '.$w.' '.$e.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string2='任选三['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][1].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][1]+1;
            $aa = '/推出'.$aa.'期';
            $string2 = preg_replace("/推出.*?期/",$aa,$string2);
        }
        //任选三2
        //中奖的3位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    array_push($z,$array[$i].' '.$array[$j].' '.$array[$s]);
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][2]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $z)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        //随机出号码
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-2);
                $w = rand($q+1,$sjn-1);
                $e = rand($w+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }
                if($w<10){
                    $w = '0'.$w;
                }
                if($e<10){
                    $e = '0'.$e;
                }
                $new1 .= $q.' '.$w.' '.$e.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string3='任选三['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][2].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][2]+1;
            $aa = '/推出'.$aa.'期';
            $string3 = preg_replace("/推出.*?期/",$aa,$string3);
        }
        //任选四
        //中奖的4位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    for($k=$s+1;$k<count($array);$k++){
                        array_push($c,$array[$i].' '.$array[$j].' '.$array[$s].' '.$array[$k]);
                    }
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][3]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $c)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-3);
                $w = rand($q+1,$sjn-2);
                $e = rand($w+1,$sjn-1);
                $r = rand($e+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }

                if($w<10){
                    $w = '0'.$w;
                }

                if($e<10){
                    $e = '0'.$e;
                }
                if($r<10){
                    $r = '0'.$r;
                }
                $new1 .= $q.' '.$w.' '.$e.' '.$r.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string4='任选四['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][3].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][3]+1;
            $aa = '/推出'.$aa.'期';
            $string4 = preg_replace("/推出.*?期/",$aa,$string4);
        }
        //任选四2
        //中奖的4位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    for($k=$s+1;$k<count($array);$k++){
                        array_push($v,$array[$i].' '.$array[$j].' '.$array[$s].' '.$array[$k]);
                    }
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][4]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $v)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-3);
                $w = rand($q+1,$sjn-2);
                $e = rand($w+1,$sjn-1);
                $r = rand($e+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }

                if($w<10){
                    $w = '0'.$w;
                }

                if($e<10){
                    $e = '0'.$e;
                }
                if($r<10){
                    $r = '0'.$r;
                }
                $new1 .= $q.' '.$w.' '.$e.' '.$r.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string5='任选四['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][4].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][4]+1;
            $aa = '/推出'.$aa.'期';
            $string5 = preg_replace("/推出.*?期/",$aa,$string5);
        }
        $upString = $string1.'   '.$string2.'   '.$string3.'   '.$string4.'   '.$string5;

        DB::table('admin_game')->where('游戏中文名',$table)->update(['号码推荐'=>$upString]);

    }
    /*
     * 获取每期开奖号码的单号遗漏
     */
    public  function getMissing($table,$stage){

        $bool = true;
        $zeroStart = false;
        switch ($table){
            case  '黑龙江11选5':
                $count = 12;
                break;
            case  '黑龙江快乐十分':
                $count = 21;
                break;
            case  '吉林快3':
                $count = 7;
                $bool = false;
                break;
            case  '吉林时时彩':
                $count = 10;
                $bool = false;
                $zeroStart = true;
                break;
            case  '四川金7乐':
                $count = 13;
                break;
            case  '四川快乐12':
                $count = 13;
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }
        $newWin = DB::table($table)->where('期号',$stage)->select('开奖号','期号','ID')->first();
        $rightNum = explode('-',$newWin->开奖号);
        if($bool == false){
            $rightNum = str_split( $newWin->开奖号);
        }
        if($zeroStart === true){
            for($i = 0;$i<$count;$i++){
                if(in_array($i,$rightNum)){
                    $r[$i]=0;
                }else{
                    $s = $i;
                    if($i<10){

                        $s = '0'.$i;
                        if($bool == false){
                            $s = $i;
                        }
                    }
                    $num = DB::table($table)->where('开奖号','like','%'.$s.'%')->where('期号','<',$stage)->select('ID')->orderby('期号','desc')->first();

                    $r[$i] = $newWin->ID-$num->ID;
                }
            }
        }else{
            for($i = 1;$i<$count;$i++){
                if(in_array($i,$rightNum)){
                    $r[$i]=0;
                }else{
                    $s = $i;
                    if($i<10){

                        $s = '0'.$i;
                        if($bool == false){
                            $s = $i;
                        }
                    }
                    $num = DB::table($table)->where('开奖号','like','%'.$s.'%')->where('期号','<',$stage)->select('ID')->orderby('期号','desc')->first();

                    $r[$i] = $newWin->ID-$num->ID;
                }
            }
        }
        return $r;

    }
    /*
     * 重号、前三重号
     */
    public function getRepeatNum($stag,$type){

        $winNum = DB::table($type)->select('ID','开奖号')->where('期号',$stag)->first();
        $b_id = $winNum->ID-1;
        $beforeWinNum = DB::table($type)->select('开奖号')->where('ID',$b_id)->first();

        $num = explode('-',$winNum->开奖号);
        $b_num = explode('-',$beforeWinNum->开奖号);

        if($type == '吉林快3'||$type=='吉林时时彩'){
            $num = str_split($winNum->开奖号);
            $b_num = str_split($beforeWinNum->开奖号);
        }
        $c=0;
        $d=0;
        $c_num = [$b_num[0],$b_num[1],$b_num[2]];
        for($i =0;$i<count($num);$i++){
            if(in_array($num[$i],$b_num)){
                $c +=1;
            }
        }
        for($i =0;$i<3;$i++){
            if(in_array($num[$i],$c_num)){
                $d +=1;
            }
        }

        $result = [$c,$d];
        return $result;
    }

    /*
     * 跨度、前三跨度、前三和值、和值、和值大小
     */
    public function span($stag,$type){
        switch ($type){
            case '黑龙江11选5'://和值大小中间值为30
                $maxNum = 6;
                break;
            case '黑龙江快乐十分':
                $maxNum = 10;
                break;
            case '吉林快3'://和值大小  小于10为小 大于9为大
                $maxNum = 3;
                break;
            case '吉林时时彩':
                $maxNum = 4;
                break;
            case '四川金7乐':
                $maxNum = 6;
                break;
            case '四川快乐12':
                $maxNum = 6;
                break;

        }
        $winNum = DB::table($type)->select('ID','开奖号')->where('期号',$stag)->first();
        $num = explode('-',$winNum->开奖号);
        $sumSize = '没有此功能';
        //处理和值大小
        if($type == '黑龙江11选5'){
            $sumSizeN =  array_sum($num);
            if($sumSizeN > 30){
                $sumSize = '大';
            }else if($sumSizeN < 30){
                $sumSize = '小';
            }else{
                $sumSize = '30';
            }
        }
        if($type == '吉林快3'){
            $sumSizeN =  array_sum($num);
            if($sumSizeN > 9){
                $sumSize = '大';
            }
            if($sumSizeN < 10){
                $sumSize = '小';
            }
        }
        if($type == '吉林快3'||$type=='吉林时时彩'){
            $num = str_split($winNum->开奖号);
        }
        $odd =0;
        $bigNum=0;
        for($m=0;$m<count($num);$m++){
            if($num[$m]%2==1){
                $odd = $odd+1;
            }
            if($num[$m]>$maxNum){
                $bigNum = $bigNum +1;
            }
        }

        $beforeNum = [$num[0],$num[1],$num[2]];
        $sum = $num[0]+$num[1]+$num[2];
        $sumAll = array_sum($num);
        sort($num);
        sort($beforeNum);

        $span =end($num)-$num[0];
        $beforeSpan =end($beforeNum)-$beforeNum[0];

        $result = [$span,$beforeSpan,$sum,$sumAll,$odd,$bigNum,$sumSize];
        return $result;
    }

    /*
     * 吉林时时彩、前、中、后、和尾单双，和尾大小，后三位跨度，和尾
     */
    public function jiLinShiShiCai($stage )
    {
        $isBigBefore = '小';
        $isBigMiddle = '小';
        $isBigAfter = '小';

        $isDoubleBefore = '双';
        $isDoubleMiddle = '双';
        $isDoubleAfter = '双';
        $data = DB::table('吉林时时彩')->where('期号', $stage)->first();
        $winNum = $data->开奖号;
        $winNumArray = str_split($winNum);
        $b_sum = $winNumArray[0] + $winNumArray[1] + $winNumArray[2];
        $m_sum = $winNumArray[1] + $winNumArray[2] + $winNumArray[3];
        $a_sum = $winNumArray[2] + $winNumArray[3] + $winNumArray[4];

        $b_sum_f = substr($b_sum, -1);
        if ($b_sum_f > 4) {
            $isBigBefore = '大';
        }
        if ($b_sum_f % 2 == 1) {
            $isDoubleBefore = '单';
        }

        $m_sum_f = substr($m_sum, -1);
        if ($m_sum_f > 4) {
            $isBigMiddle = '大';
        }
        if ($m_sum_f % 2 == 1) {
            $isDoubleMiddle = '单';
        }

        $a_sum_f = substr($a_sum, -1);
        if ($a_sum_f > 4) {
            $isBigAfter = '大';
        }
        if ($a_sum_f % 2 == 1) {
            $isDoubleAfter = '单';
        }
        $newArray = [$winNumArray[2], $winNumArray[3], $winNumArray[4]];
        sort($newArray);
        $spanAfter = $newArray[2] - $newArray[0];


        $result = [$isDoubleBefore, $isDoubleMiddle, $isDoubleAfter, $isBigBefore, $isBigMiddle, $isBigAfter, $spanAfter, $b_sum_f, $m_sum_f, $a_sum_f];
        return $result;
    }

}

function obj2arr ($obj){
    return json_decode(json_encode($obj),true);
}